(function(){
    _$(document).ready(function(){
        function App(){};
        App.prototype.ajaxFn=function(){
            if(session){
                //登录后获取书本是否已经加入书架，没加入书架就尚未阅读
                _$.post('/getBookshelfType',{bid:bid},function(data){
                    if(data['code']==1){
                        _$("#btnAddToBookshelf").html("已在书架");
                        if(data.info==null){

                        }else{
                            if(data.info.chapterName=="尚未阅读"){

                            }else{
                                _$("#btnReadBook").html("继续阅读").attr('href',"/look/"+bid+"?chapter="+data.info.chapter);
                            }
                        }
                    }else{
                        console.log(data['msg']);
                        hcyd();
                    }
                });
            }
            //书本增加点击数
            _$.post('/getBookClickNum',{bid:bid},function(json){});
            //获取书本信息
            _$.post('/getbookInfo',{bid:bid},function(data){
                console.log(data);
                var stateType="";
                if(data['code']==1){
                    var info=data['info']['bookInfo'];
                    var ainfo=data['info']['ainfo'];
                    var brinfo=data['info']['brinfo'];
                    var brimg=data['info']['brni']['brimg'];
                    var brnick=data['info']['brni']['brnick'];
                    for(var i=0;i<brinfo.length;i++){
                        brinfo[i].imgurl=brimg[i];
                        brinfo[i].nickname=brnick[i];
                    }
                    if(info!=""||info!=null||info!=undefined){
                        _$("#title,#bookTitle").html(info['bName']);
                        _$("title").html(info['bName']+"_"+info['bAuth']+"著_异世大陆小说_"+info['bName']+"txt下载_"+info['bName']+"无弹窗_起点中文网");
                        _$("#meta1").html(info['bName']+"官方首发,"+info['bName']+"小说,"+info['bName']+"最新章节,"+info['bName']+"txt下载,"+info['bName']+"无弹窗,超级浮空城吧,"+info['bName']+"离线完本");
                        _$("#meta2").html(info['bName']+","+info['bName']+"小说阅读,异世大陆小说"+info['bName']+"由作家"+info['bAuth']+"创作,起点中文网提供"+info['bName']+"首发最新章节及txt下载,"+info['bName']+"最新更新章节,更多精彩尽在起点中文网。");
                        _$("#bookDetailWrapper")
                            .attr('data-book-id',info['bid']).attr('data-cat-id',info['catId'])
                            .attr('data-author-id',info['cid']).attr('data-book-type',info['stateType']);
                        _$("#cover,#coverBlur").attr('src',info['imgurl']).attr('alt',info['bName']);
                        _$("#bAuth").html(info['bAuth']);
                        _$("#cate").html(info['cat']+"/"+info['subCateName']);
                        if(info['stateType']=="T"){
                            stateType="连载";
                        }else{
                            stateType="完结";
                        }
                        _$("#stateType").html(stateType);
                        if(info['cnt']>10000){
                            info['cnt']=info['cnt']/10000;
                            _$("#cnt").html(info['cnt'].toFixed(2)+"万字");
                        }else{
                            _$("#cnt").html(info['cnt']+"字");
                        }
                        _$("#content").html(info['desc']);
                        if(_$("#content").height()<(5.92*16)){
                            _$("#bookSummary").removeClass('enabled');
                            _$(".book-summary-more").hide();
                        }
                        if(info['ariaFansNum']>10000){
                            info['ariaFansNum']=info['ariaFansNum']/10000;
                            _$("#ariaFansNum").html(info['ariaFansNum'].toFixed(2)+"万");
                        }else{
                            _$("#ariaFansNum").html(info['ariaFansNum']);
                        }
                        _$("#bookMonth").html(info['month']);
                        _$("#bookrecomm").html(info['recomm']);
                        _$("#reward").html(info['reward']);
                        _$("#grade,#grade1").html("Lv."+ainfo['grade']);
                        _$("#author").html(ainfo['bAuth']);
                        _$("#authorAvatar").attr('src',ainfo['imgurl']).attr('alt',ainfo['bAuth']+"的头像");
                        _$("#authorDesc").html(ainfo['desc']);
                        _$("#authorA").attr('href',"/author/"+ainfo['cid']).attr('data-author-id',ainfo['cid']);
                        _$("#updateTime").html(getTimes(info['updateTime']));
                        _$("#updateTitle").html(info['updateTitle']);
                        if(brinfo.length>0){
                            _$("#bookreview").html("");
                            _$("#gdsp").hide();
                            for(var val in brinfo){
                                //console.log(brinfo[val]);
                                brinfo[val]['startTime']=getTimes(brinfo[val]['startTime']);
                                var html = template('reviewtpl', brinfo[val]);
                                _$("#bookreview").append(html);
                            }
                        }else{
                            _$("#bookreview").html('<div class="null-x"><div class="null">近期无书评记录</div></div>');
                            _$("#gdsp").hide();
                        }
                        brFn();
                    }
                }else{
                    tip(data['msg']);
                    setTimeout(function(){
                        location.href="/error";
                    },500);
                }
            });
            _$.ajax({
                type: 'post',
                url: '/getforumCount',
                data: {bid: bid},
                async: false,
                success: function (json) {
                    if(json.code==1){
                        var count=json['count'];
                        if(count>10000){
                            count=count/10000;
                            _$("#ariaCommNum").html(count.toFixed(2)+"万");
                            _$("#bookCount").html(count.toFixed(2)+"人评过");
                        }else{
                            _$("#ariaCommNum").html(count);
                            _$("#bookCount").html(count+"人评过");
                        }
                    }
                }
            });
            _$.post('/getuserHeadimg',function(data){
                if(data['code']==1){
                    var userinfo=data['info']['userinfo'];
                    var username=data['info']['username'];
                    _$("#mycover").attr('src',userinfo['imgurl']);
                    _$("#xshup").attr('data-myuser-id',username['uid']);
                    _$("#touMonth").html(userinfo['month']).attr('data-num',userinfo['month']);
                    _$("#touRecomm").html(userinfo['recomm']['nan']).attr('data-num',userinfo['recomm']['nan']);
                    var newnum=null;
                    if(userinfo['account']>=10000){
                        newnum=(userinfo['account']/10000).toFixed(2)+"万";
                    }else{
                        newnum=userinfo['account'];
                    }
                    _$("#touReward").html(newnum).attr('data-num',userinfo['account']);
                }
            });
            _$.post('/getuserTicket',{bid:bid,uid:uid},function(data){
                if(data['code']==1){
                    if(data['info']==""||data['info']==null){
                        if(_$("#wwsb")){
                            _$("#wwsb").html('未上榜');
                        }
                    }else{
                        var info=data['info'];
                        if (info['reward'] >= 100000) {
                            info['title'] = "盟主"
                        } else if (info['reward'] >= 50000 && info['reward'] < 100000) {
                            info['title'] = "掌门"
                        } else if (info['reward'] >= 40000 && info['reward'] < 50000) {
                            info['title'] = "长老"
                        } else if (info['reward'] >= 30000 && info['reward'] < 40000) {
                            info['title'] = "护法"
                        } else if (info['reward'] >= 20000 && info['reward'] < 30000) {
                            info['title'] = "堂主"
                        } else if (info['reward'] >= 10000 && info['reward'] < 20000) {
                            info['title'] = "舵主"
                        } else if (info['reward'] >= 5000 && info['reward'] < 10000) {
                            info['title'] = "执事"
                        } else if (info['reward'] >= 1000 && info['reward'] < 5000) {
                            info['title'] = "弟子"
                        } else if (info['reward'] >= 100 && info['reward'] < 1000) {
                            info['title'] = "门徒"
                        } else if (info['reward'] >= 0 && info['reward'] < 100) {
                            info['title'] = "路人"
                        }
                        _$("#wwsb").html(info['title']);
                    }
                }
            });
            _$.post('/getxsmfBook',function(data){
                console.log(data);
                var info=data['info'];
                if(data['code']==1){
                    if(info.length>0){
                        for(var val in info){
                            info[val]['index']=parseInt(val)+1;
                            var html = template('rmxstpl', info[val]);
                            $("#rmtj ul").append(html);
                        }
                    }
                }
            });
            _$.post('/getariaFansNums',{bid:bid},function(data){
                if(data['code']==1){
                    _$("#afn").html(data['count']);
                }
            });
            _$.post('/getariaFansNum',{bid:bid},function(data){
                if(data['code']==1){
                    var arrAriaFans=data['info']['arrAriaFans'];
                    var arrAriaFansInfo=data['info']['arrAriaFansInfo'];
                    var obj=_$.extend( true, arrAriaFans, arrAriaFansInfo );
                    console.log(obj);
                    if(obj.length>0) {
                        for (var val in obj) {
                            obj[val]['index'] = parseInt(val) + 1;
                            if (obj[val]['reward'] >= 100000) {
                                obj[val]['title'] = "盟主"
                            } else if (obj[val]['reward'] >= 50000 && obj[val]['reward'] < 100000) {
                                obj[val]['title'] = "掌门"
                            } else if (obj[val]['reward'] >= 40000 && obj[val]['reward'] < 50000) {
                                obj[val]['title'] = "长老"
                            } else if (obj[val]['reward'] >= 30000 && obj[val]['reward'] < 40000) {
                                obj[val]['title'] = "护法"
                            } else if (obj[val]['reward'] >= 20000 && obj[val]['reward'] < 30000) {
                                obj[val]['title'] = "堂主"
                            } else if (obj[val]['reward'] >= 10000 && obj[val]['reward'] < 20000) {
                                obj[val]['title'] = "舵主"
                            } else if (obj[val]['reward'] >= 5000 && obj[val]['reward'] < 10000) {
                                obj[val]['title'] = "执事"
                            } else if (obj[val]['reward'] >= 1000 && obj[val]['reward'] < 5000) {
                                obj[val]['title'] = "弟子"
                            } else if (obj[val]['reward'] >= 100 && obj[val]['reward'] < 1000) {
                                obj[val]['title'] = "门徒"
                            } else if (obj[val]['reward'] >= 0 && obj[val]['reward'] < 100) {
                                obj[val]['title'] = "路人"
                            }
                            var html = template('ariafanstpl', obj[val]);
                            _$("#ariaFans").append(html);
                        }
                    }else{
                        _$("#ariaFans").html("<div style='width: 100%;line-height: 106px;text-align: center;'>暂无粉丝记录</div>");
                    }
                }else{
                    _$("#ariaFans").html("<div style='width: 100%;line-height: 106px;text-align: center;'>暂无粉丝记录</div>");
                }
            });
            _$.post('/getBookTicket',{},function(data){


            });

            function brFn(){
                _$("#bookreview .book-li").click(function(){
                    location.href="/book/thread/"+bid+"?brid="+_$(this).attr('data-thread-id');
                });
            }
        };
        App.prototype.eventFn=function(){
            _$(".book-summary-more").click(function(){
               _$(this).hide();
                _$("#bookSummary").removeClass('enabled');
            });
            _$("#monthList li").click(function(){
                var index=_$(this).index();
                _$(this).children('.popup-ticket-a').addClass('checked').end().siblings('li').children('.popup-ticket-a').removeClass('checked');
                var output=parseInt(_$(this).find('.popup-ticket-a output').attr('data-num'));
                var monthnum=parseInt(_$("#touMonth").attr('data-num'));
                if(index==7){
                    _$("#tMonthNum").html(monthnum).attr('data-num',monthnum);
                }else{
                    _$("#tMonthNum").html(output).attr('data-num',output);
                }
                if(output>monthnum){
                    _$("#tMonth").addClass('disabled');
                    _$("#tMonth").prev('.popup-pay-tip').css('display','inline-block');
                }else{
                    _$("#tMonth").removeClass('disabled');
                    _$("#tMonth").prev('.popup-pay-tip').css('display','none');
                }
            });
            _$("#recommList li").click(function(){
                var index=_$(this).index();
                _$(this).children('.popup-ticket-a').addClass('checked').end().siblings('li').children('.popup-ticket-a').removeClass('checked');
                var output=parseInt(_$(this).find('.popup-ticket-a output').attr('data-num'));
                var monthnum=parseInt(_$("#touRecomm").attr('data-num'));
                if(index==9){
                    _$("#tRecommNum").html(monthnum).attr('data-num',monthnum);
                }else{
                    _$("#tRecommNum").html(output).attr('data-num',output);
                }
                if(output>monthnum){
                    _$("#tRecomm").addClass('disabled');
                    _$("#tRecomm").prev('.popup-pay-tip').css('display','inline-block');
                }else{
                    _$("#tRecomm").removeClass('disabled');
                    _$("#tRecomm").prev('.popup-pay-tip').css('display','none');
                }
            });
            _$("#accountList li").click(function(){
                var index=_$(this).index();
                _$(this).children('.popup-ticket-a').addClass('checked').end().siblings('li').children('.popup-ticket-a').removeClass('checked');
                var output=parseInt(_$(this).find('.popup-ticket-a output').attr('data-num'));
                var monthnum=parseInt(_$("#touReward").attr('data-num'));
                var newnum=null;
                if(output>=10000){
                    newnum=(output/10000)+"万";
                }else{
                    newnum=output;
                }
                _$("#tRewardNum").html(newnum).attr('data-num',output);
                if(output>monthnum){
                    _$("#tReward").addClass('disabled');
                    _$("#tReward").prev('.popup-pay-tip').css('display','inline-block');
                }else{
                    _$("#tReward").removeClass('disabled');
                    _$("#tReward").prev('.popup-pay-tip').css('display','none');
                }
            });
            //投月票点击
            _$("#tMonth").click(function(){
                if(_$(this).hasClass('disabled')){

                }else{
                    _$.post(
                        '/addSetMonth',
                        {
                            month:_$("#tMonthNum").attr('data-num'),
                            bid:bid
                        },
                        function(json){
                            if(json.code==1){
                                tip('投月票成功');
                                setTimeout(function(){
                                    location.reload();
                                },500)
                            }
                        }
                    )
                }
            });
            //投推荐票点击
            _$("#tRecomm").click(function(){
                if(_$(this).hasClass('disabled')){

                }else{
                    _$.post(
                        '/addSetRecomm',
                        {
                            recomm:_$("#tRecommNum").attr('data-num'),
                            bid:bid
                        },
                        function(json){
                            if(json.code==1){
                                tip('投推荐票成功');
                                setTimeout(function(){
                                    location.reload();
                                },500)
                            }
                        }
                    )
                }
            });
            //打赏
            _$("#tReward").click(function(){
                if(_$(this).hasClass('disabled')){

                }else{
                    _$.post(
                        '/addSetReward',
                        {
                            reward:_$("#tRewardNum").attr('data-num'),
                            bid:bid
                        },
                        function(json){
                            if(json.code==1){
                                tip('打赏成功');
                                setTimeout(function(){
                                    location.reload();
                                },500)
                            }
                        }
                    )
                }
            });
            _$("#gdsp").attr('data-bookreview-id',bid);
            //菜单下拉
            _$("#openGuide").on("click",function(){
                var key=_$(this).attr('data-key');
                var thah=_$(this);
                if(key==1){
                    _$(this).attr('data-key','0').addClass('icon-x');
                    _$("#guide").animate({'height':'100%'},300);
                    _$("#header").removeClass('header-t');
                }else{
                    _$(this).attr('data-key','1');
                    _$("#guide").animate({'height':'0'},300);
                   setTimeout(function(){
                       thah.removeClass('icon-x');
                       _$("#header").addClass('header-t');
                   },300)
                }
            });
            //加入书架
            _$("#btnAddToBookshelf").click(function(){
                _$.post("/setBookShelf",{bid:bid},function(data){
                   if(data['code']==1){
                       _$("#btnAddToBookshelf").html("已在书架");
                       tip("已加入书架");
                   }else if(data['code']==2){
                       tip("已在书架上");
                   }else{
                       tip(data['msg']);
                   }
                });
            });
            //月票tap
            _$("#popupPayTab .btn-group-cell").click(function(){
                var index=_$(this).index();
                _$(this).addClass('active').siblings('.btn-group-cell').removeClass('active');
                _$("#popupPayMain .popup-pay-content").eq(index).addClass('active').siblings('.popup-pay-content').removeClass('active');
            });

            _$("#monthRecommClose").click(function(){
                _$("#monthRecomm").hide();
                _$("#monthRecomm .aside-popup").animate({'height':"0"},300);
            });
            _$("#payTicketsX .btn-group-cell").click(function(){
                var index=_$(this).index();
                _$("#monthRecomm").show();
                _$("#monthRecomm .aside-popup").animate({'height':"274px"},300);
                _$("#popupPayTab .btn-group-cell").eq(index).addClass('active').siblings('.btn-group-cell').removeClass('active');
                _$("#popupPayMain .popup-pay-content").eq(index).addClass('active').siblings('.popup-pay-content').removeClass('active');
            });
            if(session){

            }else{
                hcyd();
            }
        };
        App.prototype.init=function(){
            //事件调用
            this.ajaxFn();
            this.eventFn();
        };
        //判断缓存中是否有过阅读
        function hcyd(){
            var localRead=JSON.parse(localStorage.getItem('OLD_READ'));
            if(localRead==""||localRead==null||localRead==undefined){

            }else{
                var bidkey=true;
                var localReadObj={};
                for(var val in localRead){
                    if(localRead[val]['bid']==bid){
                        localReadObj['bName']=localRead[val]['bName'];
                        localReadObj['aid']=localRead[val]['aid'];
                        localReadObj['aName']=localRead[val]['aName'];
                        localReadObj['cid']=localRead[val]['cid'];
                        localReadObj['cName']=localRead[val]['cName'];
                        localReadObj['time']=localRead[val]['time'];
                        localReadObj['imgurl']=localRead[val]['imgurl'];
                        bidkey=false;
                        break;
                    }
                }
                if(bidkey){

                }else{
                    _$("#btnReadBook").html("继续阅读").attr('href',"/look/"+bid+"?chapter="+localReadObj.cid);
                }
            }
        }
        //程序初始化
        new App().init();
    })
})();